package com.base.cn.platform.os.controller.course.classes.reply;

import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.classes.topic.reply.ClassesTopicReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 班级话题现评论接口Controller
 *
 * @author s.li
 * @create 2018-09-17-15:18
 */
@RestController
@RequestMapping("/api/topic/reply")
public class ApiClassesTopicReplyController extends ApiBaseController {

    @Autowired
    private ClassesTopicReplyService classesTopicReplyService;

    /**
     * 获取话题下的评论列表
     * @param topicId 话题ID
     * @param rootId 根评论ID（可不传）
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/getTopicReplyList")
    public Map<String,Object> getTopicReplyList(@RequestParam("topicId")BigDecimal topicId,
                                                @RequestParam(value = "rootId",required = false,defaultValue = "0") BigDecimal rootId,
                                                @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> params = new HashMap<>();
        params.put("topicId",topicId);
        params.put("rootId",rootId);
        Map<String, Object> reply = classesTopicReplyService.findClassesTopicReplyListPage(params, currentPage, 10, true, true, false,true);
        return ResultUtil.SUCCESS(reply);
    }

    /**
     * 保存评论数据
     * @param userId 评论用户的ID
     * @param topicId 评论的话题ID
     * @param content 评论内容
     * @param targetId 评论的父现评论ID（用于子评论，如果不是子评论传0）
     * @param rootId 评论的根ID（用于子评论，也就是第一级评论的ID，如果只保存的是第一级评论则传0）
     * @return Map<String,Object>
     */
    @RequestMapping("/saveTopicReply")
    public Map<String,Object> saveTopicReply(@RequestParam("userId") BigDecimal userId,
                                             @RequestParam("topicId") BigDecimal topicId,
                                             @RequestParam("content") String content,
                                             @RequestParam(value = "targetId",required = false,defaultValue = "0") BigDecimal targetId,
                                             @RequestParam(value = "rootId",required = false,defaultValue = "0") BigDecimal rootId){
        Map<String,Object> reply = new HashMap<>();
        reply.put("userId",userId);
        reply.put("topicId",topicId);
        reply.put("targetId",targetId);
        reply.put("content",content);
        reply.put("rootId",rootId);
        return classesTopicReplyService.saveClassesTopicReply(reply,true);
    }
}
